package com.shigen.file.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.shigen.file.util.FileUtil;
import java.io.IOException;

/**
 * 文件大小的格式化
 *
 * @author shigenfu
 * @date 2022/6/15 8:57 下午
 */
public class FileSizeSerializer extends JsonSerializer<Long> {

  @Override
  public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
    if (value == null || value < 0) {
      gen.writeString("");
    } else {
      gen.writeString(FileUtil.formatFileSize(value));
    }
  }
}
